package com.zc.auth.security.core.authentication;

import com.zc.commons.user.dto.AuthorizedUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * redis token解析处理
 *
 * @author zcj
 * @version 1.0.0
 * @date 2022/5/25 17:41
 */
public class RedisTokenService implements AuthenticationTokenService<AuthorizedUser> {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String getTokenJson(String token) {
        return this.stringRedisTemplate.opsForValue().get(getFullKey(token));
    }

    @Override
    public Class<AuthorizedUser> getSupportedClass() {
        return AuthorizedUser.class;
    }

    @Override
    public void saveToken(String key, String json, long expireTime) {
        String fullKey = this.getFullKey(key);
        stringRedisTemplate.opsForValue().set(fullKey, json, expireTime, TimeUnit.SECONDS);
    }

    private String getFullKey(String str) {
        return "zc:auth:token:data:" + str;
    }

}
